package three;

import second.UserMapper;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

/**
 * 只写接口，不用写实现类的动态代理，类似mybatis的mapper
 */
public class MapperProxy implements InvocationHandler{

	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

		System.out.printf("class:%s, method:%s, args:%s, return:%s\n",
                proxy.getClass().getName(),
                method.getName(),
                Arrays.toString(args),
                method.getReturnType().getName());

		//TODO 在这里去动态实现代理类的行为
        if("save".equals(method.getName())){
            //TODO 执行sql的insert语句
        }
		return null;
	}
	
	public static void main(String args[]){

		Class<UserMapper> cla = UserMapper.class;
        UserMapper userProxy = (UserMapper)Proxy.newProxyInstance(cla.getClassLoader(), new Class[]{cla}, new MapperProxy());
        userProxy.save("tuyou", 25);
	}
}
